<script setup lang="ts">
// ypeScript中的函数可以有可选参数，通过在参数名后加`?`来标记。默认参数则是直接在参数声明时赋值
// 函数声明
function say(name: string, meeting?: string, age?: number) {
  return meeting + "--" + name + "--" + age
}
console.log(say("mike", "hi", 20))
// 函数表达式
const add = function (x: number, y: number): number {
  return x + y
}

// 默认参数则是直接在参数声明时赋值
function greet(name: string, greeting: string = "Hello"): string {
  return greeting + ", " + name
}
console.log(greet("mike"))

// arguments 剩余参数
function buildName(name1: string, name2: string, ...restName: string[]) {
  return name1 + " " + name2 + " " + restName.join(" ")
}
console.log("buildName: ", buildName("mike", "jack", "tom", "jerry"))

// 函数重载
// 定义函数 getInfo，根据传入参数的类型不同，返回不同类型的值
function getInfo(name: string): string
function getInfo(age: number): number
// 函数重载，根据参数类型不同定义不同的函数签名
function getInfo(prop: string | number): string | number {
  if (typeof prop === "string") return "hi" + prop
  if (typeof prop === "number") return prop
  return ''
}
// 调用函数并输出结果
console.log(getInfo("mike"))
console.log(getInfo(20))

</script>

<template>
</template>